package com.wanjia.estate.controller;

import com.wanjia.estate.pojo.bdc.SetDzcl;
import com.wanjia.estate.pojo.bdc.UpdateJfzt;
import com.wanjia.estate.pojo.bdc.UpdateYwzt;
import com.wanjia.estate.pojo.bdc.Ywsl;
import com.wanjia.estate.pojo.bdc.getycslbh.GetycslbhResponse;
import com.wanjia.estate.pojo.model.JsonResult;
import com.wanjia.estate.service.DzclAddDataService;
import com.wanjia.estate.service.httpservice.PropertyHttpService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ycsl")
@Tag(name = "一窗受理控制器")
@Slf4j
public class YcslController extends BaseController{

    @Autowired
    PropertyHttpService propertyHttpService;

    @Autowired
    DzclAddDataService dzclAddDataService;

    @Operation(summary = "获取一窗受理编号接口")
    @PostMapping("/ycslbh")
    public JsonResult<Object> getslbh(){
        return new JsonResult<>(OK,propertyHttpService.getycslbh().getData());
    }


    @Operation(summary = "推送业务数据给一窗")
    @PostMapping("/pushYcsl")
    public JsonResult<String> pushYcsl(@RequestBody Ywsl.Request ywslrequest){
        try {
            String tdsyqxz = ywslrequest.getData().getYwfdcq().getTdsyqxz();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = formatter.parse(tdsyqxz);
            SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
            String format = formatter2.format(date);
            ywslrequest.getData().getYwfdcq().setTdsyqxz(format);
        } catch (Exception e) {
            log.error("日期格式化异常",e);
        }
        Ywsl.Response response = propertyHttpService.pushYcsl(ywslrequest);
        Integer code = Integer.valueOf(response.getCode());
        if (code.equals(0)){
            code=200;
        }
        String data = response.getData();
        String message = response.getMessage();
        return new JsonResult<>(code,message,data);
    }

    @Operation(summary = "推送电子材料数据给一窗")
    @PostMapping("/setDzcl")
    public JsonResult<String> setDzcl(@RequestBody SetDzcl.Request dzclrequest){
        List<SetDzcl.Ywsj> ywsj = dzclrequest.getData().getYwsj();
        for (SetDzcl.Ywsj ywsj1 : ywsj) {
            String prefix = ywsj1.getPrefix();
            List<SetDzcl.Ywdzcl> ywdzcls = dzclAddDataService.DzclAddData(ywsj1.getClmc(), prefix);
            log.info("ywdzclList电子材料:{}",ywdzcls);
            if (ywsj1.getYwdzcl() == null) {
                ywsj1.setYwdzcl(ywdzcls);
            }else {
                ywsj1.getYwdzcl().addAll(ywdzcls);
            }
        }
        SetDzcl.Response response = propertyHttpService.setDzcl(dzclrequest);
        Integer code = Integer.valueOf(response.getCode());
        if (code.equals(0)){
            code=200;
        }
        String data = response.getData();
        String message = response.getMessage();
        return new JsonResult<>(code,message,data);
    }


    @Operation(summary = "修改缴费信息给一窗")
    @PostMapping("/updateJfzt")
    public JsonResult<Object> updateJfzt(@RequestBody UpdateJfzt.JfztRequest request){
        UpdateJfzt.Response response = propertyHttpService.updateJfzt(request);
        Integer code = Integer.valueOf(response.getCode());
        if (code.equals(0)){
            code=200;
        }
        String data = response.getData();
        String message = response.getMessage();
        log.debug("data{}",data);
        return new JsonResult<>(code,message,data);
    }


//    @Operation(summary = "停止办理撤件")
//    @PostMapping("/updateYwzt")
//    public JsonResult<String> updateYwzt(@RequestBody UpdateYwzt.Request request){
//        return new JsonResult<>(OK,propertyHttpService.updateYwzt(request).getData());
//    }

}
